require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsRuneWearHandler ={}
local this = RedDotsRuneWearHandler
local specialIndex = 6

function RedDotsRuneWearHandler.init()
    CloseEvent(Config.EventType.FreshHeroRuneExchange,this.update)
    ListenEvent(Config.EventType.FreshHeroRuneExchange,this.update)
    CloseEvent(Config.EventType.FreshHeroRune,this.update)
    ListenEvent(Config.EventType.FreshHeroRune,this.update)
    return this
end

function RedDotsRuneWearHandler.getFlag(...)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Rune, false) then
        local partnerId, index = ...
        local stuffRuneList = logicMgr.RuneManager.GetStuffRuneList()
        local haveStuffCommonRune = logicMgr.RuneManager.GetHaveCommonStuffRune()
        local haveStuffSpecialRune = logicMgr.RuneManager.GetHaveSpecialStuffRune()
        local emptyRuneIndexList = logicMgr.RuneManager.GetHeroStuffRuneIndexList(partnerId)
        if emptyRuneIndexList and #emptyRuneIndexList > 0 and stuffRuneList and #stuffRuneList > 0 then
            if index then
                local isLock = logicMgr.RuneManager.CheckRuneLock(partnerId,index)
                if isLock then return Config.RedDotsFlagState.None end
                local runeData = logicMgr.RuneManager.GetHeroRuneSrvInfo(partnerId, index)
                if runeData then
                    return Config.RedDotsFlagState.None
                else
                    if index == specialIndex then
                        if haveStuffSpecialRune then
                            return Config.RedDotsFlagState.Needful
                        end
                    else
                        if haveStuffCommonRune then
                            return Config.RedDotsFlagState.Needful
                        end
                    end
                end
            else
                for k,v in pairs(emptyRuneIndexList) do
                    local isLock = RuneManager.CheckRuneLock(partnerId,v)
                    if not isLock then
                        if v == specialIndex then
                            if haveStuffSpecialRune then
                                return Config.RedDotsFlagState.Needful
                            end
                        else
                            if haveStuffCommonRune then
                                return Config.RedDotsFlagState.Needful
                            end
                        end
                    end
                end
                return Config.RedDotsFlagState.None
            end
        else
            return Config.RedDotsFlagState.None
        end
    end
    return Config.RedDotsFlagState.None
end

function RedDotsRuneWearHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.RuneWear)
end